<template>
  <div class="app-container" v-if="homeFlag">
    <!-- 数据 -->
    <el-card>
      <div class="box-card-one">
        <div class="card-item">
          <img src="@/assets/logo/logo.png" alt="">
          <div class="content">
            <span class="nub">{{ staticNumber.count }}</span>
            <span class="txt">资产总数</span>
          </div>
        </div>
        <div class="card-item">
          <img src="@/assets/logo/logo.png" alt="">
          <div class="content">
            <span class="nub">{{ staticNumber.moneyArea }}</span>
            <span class="txt">资产总面积</span>
          </div>
        </div>
        <div class="card-item">
          <img src="@/assets/logo/logo.png" alt="">
          <div class="content">
            <span class="nub">{{ staticNumber.freeRate }}</span>
            <span class="txt">闲置率</span>
          </div>
        </div>
        <div class="card-item">
          <img src="@/assets/logo/logo.png" alt="">
          <div class="content">
            <span class="nub">{{ staticNumber.contractCount }}</span>
            <span class="txt">合同总数</span>
          </div>
        </div>
        <div class="card-item">
          <img src="@/assets/logo/logo.png" alt="">
          <div class="content">
            <span class="nub">{{ staticNumber.contractSumMoney }}</span>
            <span class="txt">合同总金额</span>
          </div>
        </div>
      </div>
    </el-card>
    <el-card style="margin:  10px 0;">
      <div>合同逾期个数： {{ staticNumber.contractOverCount }}</div>
      <div class="box-card">
        <el-card class="box-card-two" shadow="never">
          <div>欠租金额：{{ staticNumber.rentMoney }}</div>
        </el-card>
        <el-card class="box-card-two" shadow="never">
          <div>累计逾期金额：{{ staticNumber.overdueMoney }}</div>
        </el-card>
        <el-card class="box-card-two" shadow="never">
          <div>累计违约金金额：{{ staticNumber.allDefaultMoney }}</div>
        </el-card>
      </div>
    </el-card>
    <!-- 折线 -->
    <el-card>
      <div ref="line" style="width: 100%;min-height: 360px;"></div>
    </el-card>
    <!-- 柱状 -->
    <el-card style="margin:  10px 0;;">
      <div style="display: flex; align-items: center;">
        <div ref="barOne" style="width: 50%;min-height: 360px;"></div>
        <div ref="barTwo" style="width: 50%;min-height: 360px;"></div>
      </div>
    </el-card>
    <!-- 饼图 -->
    <el-card>
      <div style="display: flex; align-items: center;">
        <div ref="pieOne" style="width: 33%;min-height: 360px;"></div>
        <div ref="pieTwo" style="width: 33%;min-height: 360px;"></div>
        <div ref="pieThree" style="width: 33%;min-height: 360px;"></div>
      </div>
    </el-card>
  </div>
  <div v-else>
    <h1>欢迎使用金控不动产管理（一期）系统</h1>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import {
  getChange,
  getChange2,
  getCount,
  getReceivable,
  getStatusBar,
  getStatusBar2,
  getStatusBar3
} from '@/api/menu';
import auth from '@/plugins/auth'

require('echarts/theme/macarons') // echarts theme
export default {
  name: "Index",
  data() {
    return {
      homeFlag: true,
      staticNumber: {
        "count": 0, //资产总数
        "moneyArea": 0, //资产总面积
        "freeRate": 0, //闲置率
        "contractCount": 0, //合同数量
        "contractSumMoney": 0, //合同总金额
        "contractOverCount": 0, //合同逾期个数
        "rentMoney": 0.0, //欠租金额
        "overdueMoney": 0.0, //累计逾期金额
        "allDefaultMoney": 0.0 //累计违约金金额
      }
    };
  },
  mounted() {
    this.homeFlag = auth.hasPermi('system:home:data')
    console.log(this.homeFlag)
    if (this.homeFlag) {
      this.getInitData()
      this.initLine()
      this.initBar()
      this.initPie()
    }

  },
  methods: {
    getInitData() {
      getCount().then(res => {
        this.staticNumber = res.data
      })
    },
    initLine() {

      getReceivable().then(res=>{
        const options = {
          title: {
            text: '资产应收租金情况图'
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['应收租金金额', '实收租金金额']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          toolbox: {
            feature: {
              saveAsImage: {}
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '应收租金金额',
              type: 'line',
              stack: 'Total',
              data: res.data[0]
            },
            {
              name: '实收租金金额',
              type: 'line',
              stack: 'Total',
              data: res.data[1]
            }
          ]
        }
        echarts.init(this.$refs.line).setOption(options)
      })

    },
    initBar() {
      getChange().then(res => {
        const options = {
          title: {
            text: '租户数量变化图',
            // subtext: 'Living Expenses in Shenzhen'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月',
                '十一月', '十二月'],
              axisTick: {
                alignWithLabel: true
              }
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '租户数量',
              type: 'bar',
              barWidth: '60%',
              data: res.data
            }
          ]
        };
        echarts.init(this.$refs.barOne).setOption(options)

      })
      getChange2().then(res => {
        const options2 = {
          title: {
            text: '资产数量情况图',
            // subtext: 'Living Expenses in Shenzhen'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
              axisTick: {
                alignWithLabel: true
              }
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '资产数量',
              type: 'bar',
              barWidth: '60%',
              data: res.data
            }
          ]
        };
        echarts.init(this.$refs.barTwo).setOption(options2)

      })


    },
    initPie() {
      getStatusBar().then(res => {
        const options = {
          title: {
            text: '资产状态情况图',
            // subtext: 'Fake Data',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            orient: 'vertical',
            left: 'left'
          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: '50%',
              data: res.data.map(item => ({
                ...item,
                value: item.count
              })) || [],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }
        echarts.init(this.$refs.pieOne).setOption(options)
      })
      getStatusBar2().then(res => {
        console.log('🚀 ~ file: index.vue:107 ~ getStatusBar ~ res:', res)
        const options2 = {
          title: {
            text: '自有资产情况图',
            // subtext: 'Fake Data',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            orient: 'vertical',
            left: 'left'
          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: '50%',
              data: res.data.map(item => ({
                ...item,
                value: item.count
              })) || [],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }
        echarts.init(this.$refs.pieTwo).setOption(options2)
      })
      getStatusBar3().then(res => {
        console.log('🚀 ~ file: index.vue:107 ~ getStatusBar ~ res:', res)

        const options3 = {
          title: {
            text: '代管资产情况图',
            // subtext: 'Fake Data',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            orient: 'vertical',
            left: 'left'
          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: '50%',
              data: res.data.map(item => ({
                ...item,
                value: item.count
              })) || [],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }
        echarts.init(this.$refs.pieThree).setOption(options3)
      })
    }
  },
};
</script>

<style scoped lang="scss">
.box-card-one {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-width: 980px;

  .card-item {
    flex: 1;
    display: flex;
    align-items: center;
    margin-right: 50px;
    border-right: 2px solid #f0f0f0;

    img {
      width: 50px;
      height: 50px;
      margin-right: 20px;
    }

    .content {
      display: flex;
      flex-direction: column;

      font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

      .nub {
        font-weight: 600;
        font-size: 24px;
      }

      .txt {
        font-size: 14px;
        color: #666;
      }
    }
  }

  .card-item:last-child {
    border-right: none;
  }
}

.box-card {
  margin: 20px 0;
  display: flex;
  align-items: center;

  .box-card-two {
    flex: 1;
    margin-right: 20px;
  }

  .box-card-two:last-child {
    margin-right: 0;
  }
}
h1{
  text-align: center;
  line-height: 80vh;
  height: 100vh;
}
</style>
